<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class ResidentController extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('pagination');
	}
	
	function loadAddResidentView()
	{
		if(!isset($_SESSION["session_id"])) $this->load->view('ErrorNotLoggedInView');
		else{
		$this->load->model('Admin_model');
		$data['details'] = $this->Admin_model->get_details('ACCI Dormitory');
		$data['title'] = 'Add Resident';
		$data['jsSnippet'] = 'jsSnippets/addResidentJsSnippet';
		$data['htmlSnippet'] = 'htmlSnippets/addResidentHtmlSnippet';
		$this->load->view('generic/GenericView',$data);}
	}
	
	function addResident($student_number)
	{
		if(!isset($_SESSION["session_id"])) $this->load->view('ErrorNotLoggedInView');
		else{
		$_POST['student_number']=$student_number;
		$this->load->model('Resident_model');
		if($this->Resident_model->check_resident($_POST)){
			show_error('A Resident with that student number already exists!');
		}else{
			$this->Resident_model->add_resident($_POST);
			redirect('residentController/loadSearchResidentView/'.$student_number);
		}}
	}
	
	function loadResidentToSearchView()
	{
		if(!isset($_SESSION["session_id"])) $this->load->view('ErrorNotLoggedInView');
		else{
		$offset=$this->uri->segment(3);
        $limit=5;
        $this->db->limit($limit, $offset);
        $query = $this->db->get('resident_basic_info');

        $config['base_url'] = 'http://localhost/axis/index.php/residentController/loadResidentToSearchView/';
        $config['total_rows'] = $this->db->count_all('resident_basic_info');
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $paginator=$this->pagination->create_links();
        $data['query']=$query;
		$data['paginator']=$paginator;
		$data['title'] = 'Search Resident';
		$this->load->model('Admin_model');
		$data['details'] = $this->Admin_model->get_details('ACCI Dormitory');
		$data['jsSnippet'] = 'jsSnippets/residentToSearchJsSnippet';
		$data['htmlSnippet'] = 'htmlSnippets/residentToSearchHtmlSnippet';
		$this->load->view('generic/GenericView',$data);}
	}
	
	function loadSearchResidentView($student_number)
	{
		if(!isset($_SESSION["session_id"])) $this->load->view('ErrorNotLoggedInView');
		else{
		$_POST['student_number']=$student_number;
		$this->load->model('Resident_model');
		$this->load->model('Appliance_model');
		$this->load->model('Payment_model');
		if($this->Resident_model->check_resident($_POST)){
			$data['info'] = $this->Resident_model->get_resident($_POST);
			$data['appInfo'] = $this->Appliance_model->get_appliances($_POST);
			$data['payInfo'] = $this->Payment_model->get_payments($_POST);
			$data['title'] = 'Search Resident';
			$this->load->model('Admin_model');
			$data['details'] = $this->Admin_model->get_details('ACCI Dormitory');
			$data['jsSnippet'] = 'jsSnippets/searchResidentJsSnippet';
			$data['htmlSnippet'] = 'htmlSnippets/searchResidentHtmlSnippet';
			$this->load->view('generic/GenericView',$data);
		}else{
			show_error("Student not found!");
		}}
	}
	
	function loadEditResidentView($student_number)
	{
		if(!isset($_SESSION["session_id"])) $this->load->view('ErrorNotLoggedInView');
		else{
		$_POST['student_number']=$student_number;
		$this->load->model('Resident_model');
		$data['info'] = $this->Resident_model->get_resident($_POST);
		$data['title'] = 'Edit Resident';
		$this->load->model('Admin_model');
		$data['details'] = $this->Admin_model->get_details('ACCI Dormitory');
		$data['jsSnippet'] = 'jsSnippets/editResidentJsSnippet';
		$data['htmlSnippet'] = 'htmlSnippets/editResidentHtmlSnippet';
		$this->load->view('generic/GenericView',$data);}
	}
	
	function editResident($student_number)
	{
		if(!isset($_SESSION["session_id"])) $this->load->view('ErrorNotLoggedInView');
		else{
		$_POST['student_number']=$student_number;
		$this->load->model('Resident_model');
		$this->Resident_model->edit_resident($_POST);
		redirect('residentController/loadSearchResidentView/'.$student_number);}
	}
	
	function deleteResident()
	{
		if(!isset($_SESSION["session_id"])) $this->load->view('ErrorNotLoggedInView');
		else{
		$this->load->model('Resident_model');
		$this->Resident_model->delete_resident($_POST);
		redirect('adminPageController');}
	}
	
}

/* End of file residentController.php */
/* Location: ./application/controllers/residentController.php */
?>